package cn.edu.zstu.overlappingdisappear.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import java.util.ArrayList;
import java.util.List;

import cn.edu.zstu.overlappingdisappear.R;
import cn.edu.zstu.overlappingdisappear.activity.ChainActivity;
import cn.edu.zstu.overlappingdisappear.adapter.GridViewAdapter;
import cn.edu.zstu.overlappingdisappear.adapter.LeverAdapter;
import cn.edu.zstu.overlappingdisappear.base.BaseFragment;
import cn.edu.zstu.overlappingdisappear.base.LocalHelper;
import cn.edu.zstu.overlappingdisappear.bean.LeverBean;
import cn.edu.zstu.overlappingdisappear.view.CirclePageIndicator;

/**
 * @Author ZhuMingWei(986990501@qq.com)
 * @Date on 2019/11/8
 * @Version v1.0.0
 * @Company TODO
 * @Describle TODO
 */

public class LeverFragment extends BaseFragment {

    public static int item_grid_num = 20;//每一页中GridView中item的数量
    public static int number_columns = 4;//gridview一行展示的数目
    private ViewPager view_pager;
    private LeverAdapter mAdapter;
    private List<LeverBean> dataList;
    private List<GridView> gridList = new ArrayList<>();
    private CirclePageIndicator indicator;

    @Override
    public View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_lever, container, false);
    }

    @Override
    public void initView(View view) {
        //初始化ViewPager
        view_pager = view.findViewById(R.id.view_pager);
        mAdapter = new LeverAdapter();
        view_pager.setAdapter(mAdapter);
        dataList = new ArrayList<>();
        //圆点指示器
        indicator = view.findViewById(R.id.indicator);
        indicator.setVisibility(View.VISIBLE);
        indicator.setViewPager(view_pager);
    }

    public void initDatas(List<LeverBean> levers, final int curDifficult) {
        if (dataList.size() > 0) {
            dataList.clear();
        }
         if (gridList.size() > 0) {
            gridList.clear();
        }
        if(levers.isEmpty())
            return;
        //初始化数据
        for (int i = 0; i < levers.size(); i++) {
            dataList.add(levers.get(i));
        }
        //计算viewpager一共显示几页
        int pageSize = dataList.size() % item_grid_num == 0
                ? dataList.size() / item_grid_num
                : dataList.size() / item_grid_num + 1;
        for (int i = 0; i < pageSize; i++) {
            GridView gridView = new GridView(getActivity());
            GridViewAdapter adapter = new GridViewAdapter(dataList, i);
            adapter.curDifficult = curDifficult;
            adapter.setOnItemClickListener(new GridViewAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(GridViewAdapter.ViewHolder holder, int position) {
                    if(position< LocalHelper.getCurLevel(curDifficult)){
                        Intent it = new Intent(getActivity(), ChainActivity.class);
                        it.putExtra("TYPE","Chain");
                        it.putExtra("Lever",dataList.get(position));
                        startActivity(it);
                    }
                }
            });
            gridView.setGravity(Gravity.CENTER);
            gridView.setNumColumns(number_columns);
            gridView.setAdapter(adapter);
            gridList.add(gridView);
        }
        mAdapter.add(gridList);
    }

    @Override
    public void onResume() {
        super.onResume();
        mAdapter.notifyDataSetChanged();
    }
}
